package cn.getech.data.development.service.impl;

import cn.getech.data.development.entity.BdpServiceAlarmReceiver;
import cn.getech.data.development.entity.BdpServiceAlarmServices;
import cn.getech.data.development.mapper.BdpServiceAlarmMapper;
import cn.getech.data.development.mapper.BdpServiceAlarmReceiverMapper;
import cn.getech.data.development.mapper.BdpServiceAlarmServiceMapper;
import cn.getech.data.development.model.vo.BdpServiceAlarmVO;
import cn.getech.data.development.model.vo.ServiceAlarmSearchVO;
import cn.getech.data.development.model.vo.ServiceAlarmVO;
import cn.getech.data.development.utils.DataPermissionUtil;
import cn.getech.data.intelligence.common.exception.RRException;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.system.center.utils.ShiroUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.getech.data.development.entity.BdpServiceAlarm;
import cn.getech.data.development.service.BdpServiceAlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 服务告警配置表(BdpServiceAlarm)表服务实现类
 *
 * @author makejava
 * @since 2021-05-14 16:50:27
 */
@Service
public class BdpServiceAlarmServiceImpl extends ServiceImpl<BdpServiceAlarmMapper, BdpServiceAlarm> implements BdpServiceAlarmService {

    @Autowired
    private BdpServiceAlarmMapper bdpServiceAlarmMapper;

    @Autowired
    private BdpServiceAlarmServiceMapper bdpServiceAlarmServiceMapper;

    @Resource
    private BdpServiceAlarmReceiverMapper bdpServiceAlarmReceiverMapper;


    @Override
    public IPage serviceAlarmPage(ServiceAlarmSearchVO vo) {
        Page<ServiceAlarmVO> page = new Page<>(vo.getPage(),vo.getLimit());
        vo.setTenantId(ShiroUtils.getTenantId());
        //查询告警服务配置信息
        IPage<ServiceAlarmVO> iPage =  bdpServiceAlarmMapper.serviceAlarmPage(page,vo);
        List<ServiceAlarmVO> list = iPage.getRecords();
        for(ServiceAlarmVO serviceAlarmVO:list){
            //查询告警服务列表
            List<Map<String,Object>> serviceList = bdpServiceAlarmServiceMapper.selectServiceList(serviceAlarmVO.getId(),ShiroUtils.getTenantId());
            serviceAlarmVO.setServiceList(serviceList);
            //查询告警服务接收者列表
            List<Map<String,Object>> receiverList = bdpServiceAlarmReceiverMapper.selectReceiverList(serviceAlarmVO.getId(),ShiroUtils.getTenantId());
            serviceAlarmVO.setReceiverList(receiverList);
        }
        return iPage;
    }

    @Override
    @Transactional
    public synchronized void saveServiceAlarm(BdpServiceAlarmVO vo) {
        Long userId = ShiroUtils.getUserId();
        //告警名字校验
        Integer count = bdpServiceAlarmMapper.selectServiceAlarmByName(vo.getId(),vo.getName(),ShiroUtils.getTenantId());
        if(count>0){
            throw new RRException("相同名字的服务告警已经存在");
        }
        if(vo.getServiceList()==null||vo.getServiceList().size()==0){
            throw new RRException("服务列表为空");
        }
        if(vo.getReceivedType()!=4&&(vo.getReceiverList()==null||vo.getReceiverList().size()==0)){
            throw new RRException("接收人列表为空");
        }
        BdpServiceAlarm bdpServiceAlarm = vo.convert();
        bdpServiceAlarm.setTenantId(ShiroUtils.getTenantId());
        if(vo.getId()!=null){
            //删除旧告警服务列表和告警服务接收者列表
            bdpServiceAlarmServiceMapper.deleteByServiceAlarmId(vo.getId());
            bdpServiceAlarmReceiverMapper.deleteByServiceAlarmId(vo.getId());
            //更新
            bdpServiceAlarm.setUpdatePer(userId.intValue());
            bdpServiceAlarm.setUpdateTime(new Date());
            bdpServiceAlarmMapper.updateById(bdpServiceAlarm);
        }else {
            //创建
            bdpServiceAlarm.setCreatePer(userId.intValue());
            bdpServiceAlarm.setCreateTime(new Date());
            bdpServiceAlarmMapper.insert(bdpServiceAlarm);
        }
        //新增告警服务列表和告警服务接收者列表
        for(Map<String,Object> map:vo.getServiceList()){
            if(map.get("serviceId")==null) throw new RRException("告警服务列表serviceId为空");
            if(map.get("serviceName")==null) throw new RRException("告警服务列表serviceName为空");
            BdpServiceAlarmServices bdpServiceAlarmServices = new BdpServiceAlarmServices();
            bdpServiceAlarmServices.setServiceAlarmId(bdpServiceAlarm.getId());
            bdpServiceAlarmServices.setServiceId((Integer) map.get("serviceId"));
            bdpServiceAlarmServices.setServiceName(map.get("serviceName").toString());
            bdpServiceAlarmServiceMapper.insert(bdpServiceAlarmServices);
        }

        if(vo.getReceivedType()==4){
            //本人
            BdpServiceAlarmReceiver bdpServiceAlarmReceiver = new BdpServiceAlarmReceiver();
            bdpServiceAlarmReceiver.setServiceAlarmId(bdpServiceAlarm.getId());
            bdpServiceAlarmReceiver.setReceivedUserId(ShiroUtils.getUserEntity().getUserId().intValue());
            bdpServiceAlarmReceiver.setReceivedUserName(ShiroUtils.getUserEntity().getUsername());
            bdpServiceAlarmReceiverMapper.insert(bdpServiceAlarmReceiver);
        }else {
            if (vo.getReceiverList() != null) {
                for (Map<String, Object> map : vo.getReceiverList()) {
                    if (map.get("receiverId") == null) throw new RRException("接收者列表receiverId为空");
                    if (map.get("receiverName") == null) throw new RRException("接收者列表receiverName为空");
                    BdpServiceAlarmReceiver bdpServiceAlarmReceiver = new BdpServiceAlarmReceiver();
                    bdpServiceAlarmReceiver.setServiceAlarmId(bdpServiceAlarm.getId());
                    bdpServiceAlarmReceiver.setReceivedUserId((Integer) map.get("receiverId"));
                    bdpServiceAlarmReceiver.setReceivedUserName(map.get("receiverName").toString());
                    bdpServiceAlarmReceiverMapper.insert(bdpServiceAlarmReceiver);
                }
            }
        }
    }

    @Override
    public void delete(Integer id) {
        bdpServiceAlarmServiceMapper.deleteByServiceAlarmId(id);
        bdpServiceAlarmReceiverMapper.deleteByServiceAlarmId(id);
        bdpServiceAlarmMapper.deleteById(id);
    }

    @Override
    public void updateState(Integer id,Integer state){
        BdpServiceAlarm serviceAlarm = new BdpServiceAlarm();
        serviceAlarm.setId(id);
        serviceAlarm.setState(state);
        switch (state){
            case  0:bdpServiceAlarmMapper.updateById(serviceAlarm);break;
            case  1:bdpServiceAlarmMapper.updateById(serviceAlarm);break;
        }
    }

    @Override
    public List<Map<String, Object>> userList() {
        List<Map<String, Object>> list= bdpServiceAlarmMapper.selcetUserList(ShiroUtils.getTenantId());
        return list==null?new ArrayList<>():list;
    }

    @Override
    public List<Map<String, Object>> receiverList() {
        List<Map<String, Object>> list= bdpServiceAlarmReceiverMapper.selectReceiverList(null,ShiroUtils.getTenantId());
        return list==null?new ArrayList<>():list;
    }

    @Override
    public List<Map<String, Object>> groupList() {
        Long userId = ShiroUtils.getUserId();
        List<Map<String, Object>> list=new ArrayList<>();
        if(DataPermissionUtil.isAdmin(userId.intValue())){
            list= bdpServiceAlarmMapper.groupList(ShiroUtils.getTenantId());
        }else {
            list= bdpServiceAlarmMapper.groupListByUserId(userId,ShiroUtils.getTenantId());
        }
        return list==null?new ArrayList<>():list;
    }

    @Override
    public List<Map<String, Object>> serviceList(Integer id) {
        List<Map<String, Object>> list=  bdpServiceAlarmMapper.serviceList(id,ShiroUtils.getTenantId());
        return list==null?new ArrayList<>():list;
    }

}
